package io.renren.modules.app.controller;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.AppWebServiceService;
import io.renren.modules.app.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP后台调用webservice
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api("APP后台调用webservice")
public class AppWebServiceController {
    @Autowired
    private AppWebServiceService appWebServiceService;

    @Autowired
    private LoginUtils loginUtils;

    @Login
    @GetMapping("getTaskList")
    @ApiOperation("获取任务列表")
    public R getTaskList(@LoginUser UserEntity user, @RequestParam Map<String, Object> params){
        loginUtils.setLoginInfo(user,params);
        params.put("statuskey","ishandled");
        R r = appWebServiceService.getTaskList(params);
        return  r;
    }

    @GetMapping("getTaskBill")
    @ApiOperation("获取单据明细")
    public R getTaskBill(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.getTaskBill(params);
        return  r;
    }

    @GetMapping("getTaskAction")
    @ApiOperation("获取任务动作")
    public R getTaskAction(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.getTaskAction(params);
        return  r;
    }

    @GetMapping("doAgree")
    @ApiOperation("审批")
    public R doAgree(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.doAgree(params);
        return  r;
    }

    @GetMapping("doDisAgree")
    @ApiOperation("取消审批")
    public R doDisAgree(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.doDisAgree(params);
        return  r;
    }

    @GetMapping("doReject")
    @ApiOperation("驳回")
    public R doReject(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.doReject(params);
        return  r;
    }

    @GetMapping("getUserList")
    @ApiOperation("获取人员列表")
    public R getUserList(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.getUserList(params);
        return  r;
    }

    @GetMapping("getTaskButtonList")
    @ApiOperation("获取任务按钮")
    public R getTaskButtonList(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.getTaskButtonList(params);
        return  r;
    }

    @Login
    @GetMapping("getApprovedDetail")
    @ApiOperation("获取审批明细")
    public R getApprovedDetail(@LoginUser UserEntity user, @RequestParam Map<String, Object> params){
        loginUtils.setLoginInfo(user,params);
        params.put("statuskey","ishandled");
        params.put("startline",1);
        params.put("count",10);
        R r = appWebServiceService.getApprovedDetail(params);
        return  r;
    }

    @GetMapping("getPsnDetail")
    @ApiOperation("获取人员明细")
    public R getPsnDetail(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.getPsnDetail(params);
        return  r;
    }

    @GetMapping("getTaskStatusList")
    @ApiOperation("获取任务状态")
    public R getTaskStatusList(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.getTaskStatusList(params);
        return  r;
    }

    @GetMapping("getDefaultValueOfAction")
    @ApiOperation("获取动作操作界面默认值")
    public R getDefaultValueOfAction(@RequestParam Map<String, Object> params){
        R r = appWebServiceService.getDefaultValueOfAction(params);
        return  r;
    }

    @Login
    @PostMapping("doAction")
    @ApiOperation("动作执行")
    public R doAction(@LoginUser UserEntity user,@RequestBody Map<String, Object> params,@RequestParam Map<String, Object> param){ //@RequestBody Map<String, Object> params
        loginUtils.setLoginInfo(user,params);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map  = new HashMap<>();
        map.put("taskid",params.get("taskid"));
        map.put("note",params.get("note"));
        map.put("actioncode",params.get("actioncode"));
        map.put("rejectmarks",new String[] {" "});
        list.add(map);
        Map<String, Object> maplist  = new HashMap<>();
        maplist.put("groupid",params.get("groupid"));
        maplist.put("userid",params.get("userid"));
        maplist.put("list",list);
        R r = appWebServiceService.doAction(maplist);
        return  r;
    }









}
